package com.manager.plat.action;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import com.manager.plat.dao.IUserInfoDao;
import com.manager.plat.vo.UserInfoVo;
import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = -149657666008519689L;

	private Logger logger = Logger.getLogger(LoginAction.class);
	
	private UserInfoVo userInfoVo;		//用户数据VO
	private IUserInfoDao userInfoDao;	//用户信息数据
	private String errMsg;				//用户登录错误信息
	private String key;
	
	/**用户登录*/
	public String loginUser(){
		
		String time_begin = "20160903122532";
		SimpleDateFormat s_format = new SimpleDateFormat("yyyyMMddHHmmss");
		SimpleDateFormat d_format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			Date date = s_format.parse(time_begin);
			
			logger.info(String.format("[%s]转化时间-date", date));
			String s_time = d_format.format(date);
			logger.info(String.format("[%s]转化时间-time", s_time));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			logger.info(String.format("转化时间 错误"));
		}
		
		if(userInfoVo != null){
			/**从前台获得用户名*/
			String login_username = userInfoVo.getLogin_username();
			/**从前台获得用户密码*/
			String login_password = userInfoVo.getLogin_password();
			logger.info(String.format("用户[%s]登录开始", login_username));
			/**根据用户名密码判断用户是否存在,存在允许登录，不存在则不允许登录*/
			UserInfoVo userInfo = userInfoDao.getUserInfo(login_username, login_password);
			if(userInfo != null){
				HttpServletRequest request = ServletActionContext.getRequest();
				request.getSession().setAttribute("user", userInfo);
				
				logger.info(String.format("[%s]登录成功", login_username));
				return SUCCESS;
			}
			
			errMsg = "您输入的用户名或密码有误!";
			return "error";
		}
		
		errMsg = "缓存超时,请重新登录!";
		return "error";
	}
	/**客户登录*/
	public String loginPartner(){
		if(key != null && !"".equals(key)){
			/**从前台获得用户名*/
			logger.info(String.format("用户[%s]登录开始", key));
			/**根据用户名密码判断用户是否存在,存在允许登录，不存在则不允许登录*/
			UserInfoVo userInfo = userInfoDao.getUserInfoByUserKey(key);
			if(userInfo != null){
				HttpServletRequest request = ServletActionContext.getRequest();
				request.getSession().setAttribute("user", userInfo);
				
				logger.info(String.format("[%s][%s]登录成功", userInfo.getLogin_username(), key));
				return SUCCESS;
			}
			errMsg = "密匙错误,请重获得密匙!";
			return "error";
		}
		
		errMsg = "密匙为空,请获取密匙!";
		return "error";
	}
	/**资方登录*/
	public String loginCaptial(){
		if(key != null && !"".equals(key)){
			/**从前台获得用户名*/
			logger.info(String.format("用户[%s]登录开始", key));
			/**根据用户名密码判断用户是否存在,存在允许登录，不存在则不允许登录*/
			UserInfoVo userInfo = userInfoDao.getUserInfoByCaptialKey(key);
			if(userInfo != null){
				HttpServletRequest request = ServletActionContext.getRequest();
				request.getSession().setAttribute("user", userInfo);
				
				logger.info(String.format("[%s][%s]登录成功", userInfo.getLogin_username(), key));
				return SUCCESS;
			}
			errMsg = "密匙错误,请重获得密匙!";
			return "error";
		}
		
		errMsg = "密匙为空,请获取密匙!";
		return "error";
	}
	/**用户登出*/
	public String loginOut(){
		HttpServletRequest request = ServletActionContext.getRequest();
		if(request.getSession() != null){
			request.getSession().invalidate();
		}
		return SUCCESS;
	}

	
	public UserInfoVo getUserInfoVo() {
		return userInfoVo;
	}
	public void setUserInfoVo(UserInfoVo userInfoVo) {
		this.userInfoVo = userInfoVo;
	}
	public IUserInfoDao getUserInfoDao() {
		return userInfoDao;
	}
	public void setUserInfoDao(IUserInfoDao userInfoDao) {
		this.userInfoDao = userInfoDao;
	}
	public String getErrMsg() {
		return errMsg;
	}
	public void setErrMsg(String errMsg) {
		this.errMsg = errMsg;
	}
	public String getKey() {
		return key;
	}
	public void setKey(String key) {
		this.key = key;
	}
}